import {
    ConfigsResult,
} from "db://assets/proto/new-world-api/v1/system";
import Long from "long";
import {FishStatus} from "db://assets/proto/shared/v1/biz";
import {BagType} from "db://assets/script/game/constants/bag-type";
import {Color} from "cc";
import {fishConfigsLoader} from "db://assets/script/configs/fish-configs-loader";
import {propsConfigsLoader} from "db://assets/script/configs/props-configs-loader";
import {PropsConfigs} from "db://assets/proto/shared/v1/configs";
import {buildingConfigsLoader} from "db://assets/script/configs/building-configs-loader";
import {mixingConfigsLoader} from "db://assets/script/configs/mixing-configs-loader";
import {playerConfigsLoader} from "db://assets/script/configs/player-configs-loader";
import {zoneConfigsLoader} from "db://assets/script/configs/zone-configs-loader";

export class ConfigsHelper {
    private _configCore: ConfigsResult | null = null;

    _propBagTitle: Map<BagType, string> = new Map(
        [
            [BagType.PlayerBag, '宗门包裹'],
            [BagType.FishBag, '弟子包裹'],
        ]
    );

    enrichCore(configCore: ConfigsResult) {
        console.log('enrich core', configCore);
        this._configCore = configCore;
    }

    loadBuildingConfig(buildingId: Long) {
        return buildingConfigsLoader.findBuildingConfigs(buildingId)!
    }

    loadUserLevelByExp(exp: Long) {
        return playerConfigsLoader.findPlayerLevelByExp(exp)
    }

    loadUserLevelBuildingNumLimit(level: number, building: Long) {
        const limit = playerConfigsLoader.findBuildingNumLimitConfigsByLevel(level, building)
        return limit == undefined ? Number.MAX_SAFE_INTEGER : limit
    }

    loadUserLevelBuildingLevelLimit(level: number, building: Long) {
        const limit = playerConfigsLoader.findBuildingLevelLimitConfigsByLevel(level, building)
        return limit == undefined ? Number.MAX_SAFE_INTEGER : limit
    }

    loadBuildingUpgradeConfig(buildingId: Long, level: number) {
        return buildingConfigsLoader.findBuildingUpgradeConfigsByLevel(buildingId, level)
    }

    loadUserLevelConfig(level: number) {
        return playerConfigsLoader.findPlayerLevelConfigsByLevel(level)
    }

    loadPropBagTitle(bagType: BagType) {
        return this._propBagTitle.get(bagType)
    }

    loadHairStyle(styleId: number) {
        return fishConfigsLoader.findFishLooksConfig(Long.fromNumber(styleId))
    }

    loadHairColor(styleId: number) {
        return fishConfigsLoader.findFishLooksConfig(Long.fromNumber(styleId))
    }

    loadEyeColor(styleId: number) {
        return fishConfigsLoader.findFishLooksConfig(Long.fromNumber(styleId))
    }

    loadSkinColor(styleId: number) {
        return fishConfigsLoader.findFishLooksConfig(Long.fromNumber(styleId))
    }

    loadZoneConfig(zoneId: Long) {
        return zoneConfigsLoader.findZoneConfigs(zoneId)
    }

    checkResourceBuilding(buildingId: Long, level: number) {
        const brc = this.loadBuildingMixingFormulaConfigs(buildingId, level)
        return brc != undefined
    }

    loadBuildingMixingFormulaConfigs(buildingId: Long, level: number) {
        const pipeTag = 'pipe_tag:building:1:' + buildingId.toString()
        const pipeConfig = mixingConfigsLoader.findMixPipeConfig(pipeTag, level);
        if (pipeConfig) return mixingConfigsLoader.findMixFormulaConfig(pipeConfig!.formulaId)
        return undefined
    }


    loadBuildingMixingPipeConfigs(buildingId: Long, level: number) {
        const pipeTag = 'pipe_tag:building:1:' + buildingId.toString()
        return mixingConfigsLoader.findMixPipeConfig(pipeTag, level);
    }


    loadStatusStr(status: FishStatus) {
        let str = "历练"
        switch (status) {
            case FishStatus.alive:
                str = "历练"
                break;
            case FishStatus.sleep:
                str = "休沐"
                break;
            case FishStatus.up_sell:
                str = "下架"
                break;
            case FishStatus.dead:
                str = "死亡"
                break;
            case FishStatus.refined:
                str = "已炼化"
                break;
            case FishStatus.godlike:
                str = "已飞升"
                break;
            case FishStatus.moving:
                str = "赶路中.."
                break
            case FishStatus.in_battle:
                str = "战斗中.."
                break
            case FishStatus.pre_battle:
                str = "战备中.."
                break
        }
        return str
    }

    loadLevelStr(level: number) {
        const flc = fishConfigsLoader.findFishLevelConfigsByLevel(level)
        if (!flc) {
            return "飞升"
        }
        return flc.name
    }

    loadRealmIcon(level: number): string | null {
        const flc = fishConfigsLoader.findFishLevelConfigsByLevel(level)
        if (!flc) return 'textures/ui-icon/realm-icon-10/spriteFrame'
        if (flc.icon == '') return null
        else return 'textures/ui-icon/' + flc.icon + '/spriteFrame'
    }

    loadTalentName(talent: number) {
        const tc = fishConfigsLoader.findTalentConfig(talent)
        if (tc) return tc.name
        return ''
    }


    loadPersonalityName(personalityId: number) {
        return fishConfigsLoader.findPersonalityName(personalityId)
    }

    loadPeculiarityConfig(peculiarityId: Long) {
        return fishConfigsLoader.findPeculiarityConfigs(peculiarityId)
    }

    loadAssetConfig(assetId: number): PropsConfigs | undefined {
        return propsConfigsLoader.findPropConfigs(assetId)
    }

    loadBuildingAssetConfig(buildingId: Long): PropsConfigs | undefined {
        const bc = buildingConfigsLoader.findBuildingConfigs(buildingId);
        if (!bc) return undefined
        return configsHelper.loadAssetConfig(bc!.propId.toNumber())
    }

    loadTalentColor(talent: number) {
        const color = new Color(192, 192, 192)
        const tc = fishConfigsLoader.findTalentConfig(talent)
        if (tc) Color.fromHEX(color, tc!.color)
        return color
    }

    loadPeculiarityBg(quality: number) {
        return 'textures/ui-icon/quality-bg-' + Math.floor(quality / 2) + '/spriteFrame'
    }

}

export const configsHelper = new ConfigsHelper()
